package com.SmartCare.utils;

import com.SmartCare.common.enums.Constants;
import com.SmartCare.common.dto.medicine.MedicationDTO;
import com.SmartCare.common.exception.ServiceException;
import com.SmartCare.mapper.medicine.MedicationFrequencyMapper;
import com.SmartCare.pojo.medicine.Medication;
import com.SmartCare.pojo.medicine.MedicationFrequency;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用药途径转化工具
 */

@Service
public class MedicationTransToDTO {
    @Resource
    private MedicationFrequencyMapper medicationFrequencyMapper;

    /**
     * 将药品转成DTO
     * @param medication 药品
     * @return 返回DTO
     */
    public MedicationDTO trans(Medication medication) {
        MedicationFrequency fre = medicationFrequencyMapper.selectById(medication.getFrequencyId());
        if (fre == null) {
            throw new ServiceException(Constants.CODE_400, "服用频率信息不存在");
        }
        return MedicationDTO.builder()
                .id(medication.getId())
                .dosage(medication.getDosage())
                .name(medication.getName())
                .unit(medication.getUnit())
                .type(medication.getType())
                .mode(medication.getMode())
                .careHomeName(medication.getCareHomeName())
                .frequency(fre)
                .build();
    }
}
